import pytest
from pytest_practice.calculator.calculator import Calculator
from pytest_practice.calculator.datas.yamlUtil import YamlUtil, PyTestLog, DATA_PATH, LOG_PATH

test_log = PyTestLog(__name__, log_path=LOG_PATH)


def pytest_collection_modifyitems(items):
    """
    测试用例收集完成时，将收集到的item的name和nodeid的中文显示在控制台上
    :return:
    """
    for item in items:
        item.name = item.name.encode("utf-8").decode("unicode_escape")
        print(item.nodeid)
        item._nodeid = item.nodeid.encode("utf-8").decode("unicode_escape")


@pytest.fixture(autouse=True)
def get_cal():
    cal = Calculator()
    yield cal


# 使用fixture 实现setup/teardown 功能

@pytest.fixture(autouse=True)
def print_info():
    test_log.log_info('【开始计算】')
    yield
    test_log.log_info('【计算结束】')


# 使用fixture 实现参数化
@pytest.fixture(params=YamlUtil(DATA_PATH).read_test_data_by_yaml('div.P0.datas'),
                ids=YamlUtil(DATA_PATH).read_test_data_by_yaml('div.P0.ids'))
def get_div_p0_data(request):
    yield request.param


@pytest.fixture(params=YamlUtil(DATA_PATH).read_test_data_by_yaml('div.P1.datas'),
                ids=YamlUtil(DATA_PATH).read_test_data_by_yaml('div.P1.ids'))
def get_div_p1_data(request):
    yield request.param


@pytest.fixture(params=YamlUtil(DATA_PATH).read_test_data_by_yaml('div.P2.datas'),
                ids=YamlUtil(DATA_PATH).read_test_data_by_yaml('div.P2.ids'))
def get_div_p2_data(request):
    yield request.param


@pytest.fixture(params=YamlUtil(DATA_PATH).read_test_data_by_yaml('add.P0.datas'),
                ids=YamlUtil(DATA_PATH).read_test_data_by_yaml('add.P0.ids'))
def get_add_P0_data(request):
    yield request.param


@pytest.fixture(params=YamlUtil(DATA_PATH).read_test_data_by_yaml('add.P1_1.datas'),
                ids=YamlUtil(DATA_PATH).read_test_data_by_yaml('add.P1_1.ids'))
def get_add_P1_1_data(request):
    yield request.param


@pytest.fixture(params=YamlUtil(DATA_PATH).read_test_data_by_yaml('add.P1_2.datas'),
                ids=YamlUtil(DATA_PATH).read_test_data_by_yaml('add.P1_2.ids'))
def get_add_P1_2_data(request):
    yield request.param


@pytest.fixture(params=YamlUtil(DATA_PATH).read_test_data_by_yaml('add.P2.datas'),
                ids=YamlUtil(DATA_PATH).read_test_data_by_yaml('add.P2.ids'))
def get_add_P2_data(request):
    yield request.param


@pytest.fixture(scope='session', autouse=True)
def finish_test():
    yield
    test_log.log_info('\n【结束测试】\n')
